import gui from '@/lib/gui.js';
import https from '@/lib/https.js';
import config from '@/lib/config.js';

const weekLabels= [{day:1, name:'一'}, {day:2, name:'二'}, {day:3, name:'三'}, {day:4, name:'四'}, {day:5, name:'五'}, {day:6, name:'六'}, {day:0, name:'日'}];
const monthLabels = [{day:1, name:'1'}, {day:2, name:'2'}, {day:3, name:'3'}, {day:4, name:'4'}, {day:5, name:'5'},
{day:6, name:'6'}, {day:7, name:'7'}, {day:8, name:'8'}, {day:9, name:'9'}, {day:10, name:'10'},
{day:11, name:'11'}, {day:12, name:'12'}, {day:13, name:'13'}, {day:14, name:'14'}, {day:15, name:'15'},
{day:16, name:'16'}, {day:17, name:'17'}, {day:18, name:'18'}, {day:19, name:'19'}, {day:20, name:'20'},
{day:21, name:'21'}, {day:22, name:'22'}, {day:23, name:'23'}, {day:24, name:'24'}, {day:25, name:'25'},
{day:26, name:'26'}, {day:27, name:'27'}, {day:28, name:'28'}, {day:29, name:'29'}, {day:30, name:'30'},
{day:31, name:'31'}, {day:99, name:'每月最后一天'}];
const yearLabels= [{day:1, name:'1月'}, {day:2, name:'2月'}, {day:3, name:'3月'}, {day:4, name:'4月'}, {day:5, name:'5月'}, {day:6, name:'6月'}, 
{day:7, name:'7月'}, {day:8, name:'8月'}, {day:9, name:'9月'}, {day:10, name:'10月'}, {day:11, name:'11月'}, {day:12, name:'12月'}];
const hourList = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23];
const minList = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59];
const singleDayPunchTimeList=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const oneDayTime = 24 * 3600 * 1000;

function formatDate(dtDate){
  var seperator1 = "-";
  var year = dtDate.getFullYear();
  var month = dtDate.getMonth() + 1;
  var strDate = dtDate.getDate();
  if (month >= 1 && month <= 9) {
    month = "0" + month;
  }
  if (strDate >= 0 && strDate <= 9) {
    strDate = "0" + strDate;
  }
  var currentdate = year + seperator1 + month + seperator1 + strDate;
  return currentdate;
}

function formatCNDate(dtDate){
	var seperator1 = "-";
	var year = dtDate.getFullYear();
	var month = dtDate.getMonth() + 1;
	var strDate = dtDate.getDate();
	if (month >= 1 && month <= 9) {
	  month = "0" + month;
	}
	if (strDate >= 0 && strDate <= 9) {
	  strDate = "0" + strDate;
	}
	var currentdate = month + '月' + strDate + '日';
	return currentdate;
}

function parseDate(strDate){
	return new Date(strDate);
}

function getWeekLabels(){
  var arr = new Array();
  for(var i=0; i<weekLabels.length; i++){
    arr.push({day:weekLabels[i].day, name: weekLabels[i].name});
  }
  return arr;
}

function loadDayActionList(actList, dayObj){
  let dayActList = [];
  for(let i=0; actList!=null && i<actList.length; i++){
    let act = actList[i];
    if(act.setting_repeat == 'single'){
      if(act.dtsingle == dayObj.datestr){
        dayActList.push(act);
      }
    }else if(act.setting_repeat == 'fix'){
      if(act.setting_type == 'week'){
        let weekarr = act.setting_val.split(',');
        if(containsWeek(weekarr, dayObj.date)){
          dayActList.push(act);
        }
      }else if(act.setting_type == 'month'){
        let montharr = act.setting_val.split(',');
        if(containsMonth(montharr, dayObj.date)){
          dayActList.push(act);
        }
      }
    }else if(act.setting_repeat == 'nonfix'){
      if(act.setting_type == 'week'){
        if(dayObj.datestr>=act.dtstart && dayObj.datestr<act.dtend){
          dayActList.push(act);
        }
      }else if(act.setting_type == 'month'){
        if(dayObj.datestr>=act.dtstart && dayObj.datestr<act.dtend){
          dayActList.push(act);
        }
      }
    }
  }
  return dayActList;
}

function genWeekLabels(dtpoint){
	var dtstart = getWeekFirstDay(dtpoint);
	var arr = [];
	for(var i=0; i<7; i++){
		var dtday = new Date(dtstart.getTime() + 3600 * 1000 * 24 * i);
		arr.push({date: dtday, dateStr:formatDate(dtday), week: formatWeekLabel(dtday), day: dtday.getDate()});
	}
	return arr;
}

function getWeekFirstDay(dtpoint){
	return new Date(dtpoint.getTime() - (3600 * 1000 * 24 * (dtpoint.getDay() == 0?6:dtpoint.getDay()-1)));
}

function formatWeekLabel(dtdate) {
  let days = ''
  switch (dtdate.getDay()) {
    case 1:
      days = '周一';
      break;
    case 2:
      days = '周二';
      break;
    case 3:
      days = '周三';
      break;
    case 4:
      days = '周四';
      break;
    case 5:
      days = '周五';
      break;
    case 6:
      days = '周六';
      break;
    case 0:
      days = '周日';
      break;
  }
  return days;
}

function getCurrDate() {
  return new Date();
}

function getDateWithOffsetWeek(dtpoint, offsetweek) {
	return new Date(dtpoint.getTime() + 3600 * 1000 * 24 * 7 * offsetweek);
}

function getDateWithOffsetDay(dtDate, offsetday){
	return new Date(dtDate.getTime() + 3600 * 1000 * 24 * offsetday);
}

export default{
	getCurrDate,
	getDateWithOffsetWeek,
	getDateWithOffsetDay,
	getWeekFirstDay,
	genWeekLabels,
	
	formatDate,
	formatCNDate,
	parseDate,
	getWeekLabels,
	loadDayActionList
}
